/**
 * @file pintaros.h
 * @brief Main header file, contain all global definition, data structure, and function
 *
 * @author Ricky Hariady (ricky.hariady@enter.web.id)
 *
 * @date 7/3/2013
 */

#define PINTAROS_H

#define MAX_BUFFER_SIZE 255     ///< Maximum size for transmission buffer */
#define MAX_DATA_SIZE   65535   ///< Maximum size for data section of APDU */

#define CEIL(A,B)       ((A%B)==0 ? (A/B) : (A/B + 1))

/*********************************/
/*        Type Definition        */
/*********************************/

/* Typedef Section Begin */

typedef unsigned char           uint8_t;        ///< unsigned integer 8-bit
typedef short unsigned int      uint16_t;       ///< unsigned integer 16-bit
typedef unsigned int            uint32_t;       ///< unsigned integer 32-bit
typedef char                    bool;           ///< boolean

/********************************/
/*        Data Structure        */
/********************************/

/**
 * @brief structure of APDU resources
 */
struct  apdu_res_struct
{
        uint8_t         CLA;                            ///< Class of instruction
        uint8_t         INS;                            ///< Instruction code
        uint8_t         P1;                             ///< Parameter 1
        uint8_t         P2;                             ///< Parameter 2
        uint8_t         cmd_data[MAX_DATA_SIZE];	///< buffer for command data
        int             cmd_data_len;                   ///< length of command data
        uint8_t         rsp_data[MAX_DATA_SIZE];        ///< buffer for response data
        int             rsp_data_len;                   ///< length of response data
        uint16_t        rsp_type;                       ///< response code
        uint8_t         rsp_xtra_param;                 ///< extra parameter for response (SW2) if needed
};

/**
 * @brief structure of transmission buffer
 */
struct  buffer_struct
{
        uint8_t buffer[MAX_BUFFER_SIZE];   ///< actual buffer
        uint8_t length;                    ///< length of buffer used
};

/**************************/
/*        Variable        */
/**************************/

#if DEBUG
int debugLevel = 0;
#endif

struct  apdu_res_struct apdu_res;              ///< apdu resources
struct  buffer_struct   rx_buffer;             ///< buffer for Rx transmission
struct  buffer_struct   tx_buffer;             ///< buffer for Tx transmission
uint16_t                exp_data;              ///< length of command data expected by command interpreter
bool                    busy_flag;             ///< indication for transmission protocol if the main loop still busy

